package org.jhub1.system.xmpp.gateway.packet;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import org.jhub1.system.xmpp.log.LogAdapter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.provider.IQProvider;
import org.xmlpull.v1.XmlPullParser;

/**
 * Represents XMPP packets.
 *
 * @author Marek Kaszewski
 */
public class IQProviderGateway implements IQProvider {
	
	private static LogAdapter log = LogAdapter.getLog(IQProviderGateway.class);
	
	@Override
	public IQ parseIQ(XmlPullParser parser) throws Exception {
		
		IQGatewayItems gwItems = new IQGatewayItems();
		IQGatewayItems.Item item;
		@SuppressWarnings("unused")
		IQGatewayItems.Item.SubItem subItem;
		boolean done = false;
		List<SubItemable> subItems = new CopyOnWriteArrayList<SubItemable>();
		String valueT = null;
		@SuppressWarnings("unused")
		String name;
		@SuppressWarnings("unused")
		String namespace;
		String atype = null;
		String command = null;
		String id = null;
		String rq = null;
		String uri = null;
		String error = null;
		String address = null;
		String value = null;
		String param_name = null;
		name = parser.getName();
		namespace = parser.getNamespace();
		
        while (!done) {
            int eventType = parser.next();

            if (eventType == XmlPullParser.START_TAG && "jhub1data".equals(parser.getName())) {
                // Initialize the variables from the parsed XML
                id = parser.getAttributeValue("", "id");
                atype = parser.getAttributeValue("", "atype");
                command = parser.getAttributeValue("", "command");
                rq = parser.getAttributeValue("", "rq");
                uri = parser.getAttributeValue("", "uri");   
                error = parser.getAttributeValue("", "error");                
            }
            else if (eventType == XmlPullParser.START_TAG && "set".equals(parser.getName())) {
                // Initialize the variables from the parsed XML
            	address = parser.getAttributeValue("", "address");
                value = parser.getAttributeValue("", "value");
            }
            
            else if (eventType == XmlPullParser.START_TAG && "param".equals(parser.getName())) {
                // Initialize the variables from the parsed XML
            	param_name = parser.getAttributeValue("", "name");
            }
            else if (eventType == XmlPullParser.END_TAG && "param".equals(parser.getName())) {
                synchronized (subItems) {
                	IQGatewayItems.Item.SubItemParams sip = 
                			new IQGatewayItems.Item.SubItemParams(param_name, valueT);
                    subItems.add((SubItemable)sip);
                }
                valueT = new String();
            }
         
            else if (eventType == XmlPullParser.END_TAG && "set".equals(parser.getName())) {
                synchronized (subItems) {
                	IQGatewayItems.Item.SubItem si = 
                			new IQGatewayItems.Item.SubItem(address, value);
                    subItems.add((SubItemable)si);
                }
            }
            else if (eventType == XmlPullParser.TEXT && !parser.isWhitespace()) {
                valueT = parser.getText();
            }
            else if (eventType == XmlPullParser.END_TAG && "jhub1data".equals(parser.getName())) {
                // Create a new Item and add it to DiscoverItems.
                  item = new IQGatewayItems.Item(id);
                  item.setAccessType(atype);
                  item.setCommand(command);
                  item.setError(error);
                  item.setRequestType(rq);
                  item.setUri(uri);
                  if (valueT != null) {
                	  item.setText(valueT);
                	  valueT = new String();
                  }
                  if (!subItems.isEmpty()) {
                	  item.setSubItems(subItems);
                  }
                  gwItems.addItem(item);
                  subItems = new CopyOnWriteArrayList<SubItemable>();
            }
            else if (eventType == XmlPullParser.END_TAG && "query".equals(parser.getName())) {
                done = true;
            }
        }	
        log.debug("Received IQ: '" + parser.getName() + "', with namespace: '" + parser.getNamespace() + "'.");
		return gwItems;
	}
}